// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

import {getshoptype,getshoplist,getShopListByType,getShopListByinfo} from '../../utils/api'

Page({
  data: {
    typeList:[],
    list: [
      // {
      //   nickname: '苏苏',
      //   price: '10',
      //   description: '虞美人有大长腿',
      //   url: 'https://img1.baidu.com/it/u=610726533,1686239110&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750',
      //   avatar: 'https://img1.baidu.com/it/u=610726533,1686239110&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750'
      // }
      
    ],
    gname:''
  },
  //获取商品分类
  async getShoptype(){
    const {data:res} = await getshoptype()
    // console.log(res)
    if(res.data.code === 200){
      this.setData({
        typeList:res.data.data
      })
      
    }
  },
  //获取商品列表
  async getShopList(){
    const {data:res} = await getshoplist()
    // console.log(res)
    if(res.data.code == 200){
      if(res.data.data.length ==0) return
      res.data.data.map(item=>{
      item.url = JSON.parse(item.url)[0]
      })
      this.setData({
        list:res.data.data
      })
      
    }
  },
  //跳转商品详情
  viewgoods(e){
    wx.navigateTo({
      url: `/pages/goodsinfo/goodsinfo?id=${e.currentTarget.dataset.pid}`,
    })
  },
  //根据分类筛选
  async GetShopListByType(e){
    const {cid} = e.currentTarget.dataset

    const {data:res} = await getShopListByType(cid)
    console.log(res)
    if(res.data.code == 200){
      if(res.data.data.length ==0) return this.setData({
        list:[]
      })
      res.data.data.map(item=>{
      item.url = JSON.parse(item.url)[0]
      })
      this.setData({
        list:res.data.data
      })
      
    }
  },
  //获取输入内容
  changeSeache(e){
    this.setData({
      gname:e.detail.value
    })
  },
  //搜索
  async search(){
    if(this.data.gname === "") return wx.showToast({
      title: '请输入内容',
      icon: 'none',
      duration: 1000
  }); 
    const {data:res} = await getShopListByinfo({gname:this.data.gname})
    console.log(res)
    if(res.data.code == 200){
      if(res.data.data.length ==0) return this.setData({
        list:[]
      })
      res.data.data.map(item=>{
        item.url = JSON.parse(item.url)[0]
        })
        this.setData({
          list:res.data.data
        })
    }
  },

  //下拉事件
  async onPullDownRefresh(){
    const {data:res} = await getshoplist()
    if(res.data.code == 200){
      if(res.data.data.length ==0) return
      res.data.data.map(item=>{
      item.url = JSON.parse(item.url)[0]
      })
      setTimeout(()=>{
        wx.stopPullDownRefresh();
      },1000)
      this.setData({
        list:res.data.data
      })
      
    }else{
      setTimeout(()=>{
        wx.stopPullDownRefresh();
      },1000)
      wx.showToast({
        title: '网络超时，请重试',
        icon: 'none',
        duration: 2000,
        mask:true
      });
    }
  },
  onLoad(){
    this.getShoptype()
    this.getShopList()
    
    
  }
})
